//
//  WhiteDetailViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/23.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "WhiteDetailViewController.h"
#import "WhiteDetailTableCell.h"

@interface WhiteDetailViewController ()
@property (nonatomic,strong) NSMutableArray*orginData;
@property (nonatomic,strong) NSMutableArray <WhiteDetailModel*>*dataArr;

@end

@implementation WhiteDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self coginData];
}
- (void)setupUI{
    self.title = self.dappInfo.title;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
}
- (void)coginData{
    [self.dataArr removeAllObjects];
    self.orginData = self.dappInfo.whites.mutableCopy;
    [self.orginData enumerateObjectsUsingBlock:^(WhiteDappInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       __block WhiteDetailModel*listM;
        [self.dataArr enumerateObjectsUsingBlock:^(WhiteDetailModel * _Nonnull detailModel, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([detailModel.contract_name isEqualToString:obj.contract_name]) {
                listM = detailModel;
            }
        }];
        if (!listM) {
            listM = [WhiteDetailModel new];
        }
        if (!listM.contract_name.length) {
            listM.contract_name = obj.contract_name;
        }
        if ([listM.contract_name isEqualToString:obj.contract_name]) {
            listM.contract_name = obj.contract_name;
            [listM.whites addObject:obj];
            NSMutableString*str = [NSMutableString string];
            [obj.whites.allKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString*value = obj.whites[key];
                [str appendString:[NSString stringWithFormat:@"%@:%@%@",key,value.length?value:XLocalized(@"任意"),idx==obj.whites.allKeys.count-1?@"":@"\n"]];
            }];
            WhiteContentModel*content = [WhiteContentModel new];
            content.content = str.copy;
            content.height = AdaptedWidth(60)+(obj.free?AdaptedWidth(50):0.0f)+[str.copy heightForFont:kDefaultFont(12) width:kScreenWidth-AdaptedWidth(60)];
            listM.rowHeight += content.height;
            [listM.rowHeights addObject:content];
            if (![self.dataArr containsObject:listM]) {
                [self.dataArr addObject:listM];
            }
        }else{
            WhiteDetailModel*listM = [WhiteDetailModel new];
            listM.contract_name = obj.contract_name;
            [listM.whites addObject:obj];
            WhiteContentModel*content = [WhiteContentModel new];
            NSMutableString*str = [NSMutableString string];
            [obj.whites.allKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString*value = obj.whites[key];
                [str appendString:[NSString stringWithFormat:@"%@:%@%@",key,value.length?value:XLocalized(@"任意"),idx==obj.whites.allKeys.count-1?@"":@"\n"]];
            }];
            content.content = str.copy;
            content.height = AdaptedWidth(60)+(obj.free?AdaptedWidth(50):0.0f)+[str.copy heightForFont:kDefaultFont(12) width:kScreenWidth-AdaptedWidth(60)];
            [listM.rowHeights addObject:content];
            listM.rowHeight += content.height;
            [self.dataArr addObject:listM];
        }
    }];
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WhiteDetailTableCell*cell = [WhiteDetailTableCell cellForTableView:tableView];
    cell.detailModel = [self.dataArr objectAtIndex:indexPath.section];
    kWeakSelf(self);
    cell.cancelAllWhite = ^(WhiteDetailModel * _Nonnull allWhite) {
        DLog(@"取消白名单");
        for (WhiteDappInfo *info in allWhite.whites) {
            if ([weakself.orginData containsObject:info]) {
                [weakself.orginData removeObject:info];
            }
        }
        [weakself updateWalletWhites];
    };
    cell.cancelFree = ^(WhiteDappInfo * _Nonnull white) {
        DLog(@"取消免弹窗");
        white.free = NO;
        weakself.dappInfo.whites = weakself.orginData.copy;
        [weakself updateWalletWhites];
    };
    cell.cancelWhite = ^(WhiteDappInfo * _Nonnull white) {
        DLog(@"取消动作");
        if ([weakself.orginData containsObject:white]) {
            [weakself.orginData removeObject:white];
        }
        [weakself updateWalletWhites];
    };
    return cell;
}

- (void)updateWalletWhites{
    self.dappInfo.whites = self.orginData.copy;
    NSMutableArray*orginData = self.wallet.whiteDapps.mutableCopy;
    [orginData enumerateObjectsUsingBlock:^(DappInfo *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.permissions isEqualToString:self.dappInfo.permissions] &&[obj.dappId isEqualToString:self.dappInfo.dappId] ) {
            if (self.dappInfo.whites.count) {
                [orginData replaceObjectAtIndex:idx withObject:self.dappInfo];
            }else{
                [orginData removeObject:obj];
            }
            *stop = YES;
        }
    }];
    self.wallet.whiteDappJson = [orginData modelToJSONString];
    [[XHDataBaseManager shareInstance]updateWalletToDB:self.wallet];
    [self coginData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WhiteDetailModel* detailModel = [self.dataArr objectAtIndex:indexPath.section];
    return detailModel.rowHeight+AdaptedWidth(160);
}
- (NSMutableArray<WhiteDetailModel*>*)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
